package com.chpro.scsw.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.chpro.common.JSONUtils;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.chpro.scsw.model.ArticleModel;
import com.chpro.scsw.model.User;
import com.chpro.scsw.service.IArticleService;
import com.chpro.scsw.service.IMenuService;
import lombok.extern.slf4j.Slf4j;
@Controller
@Log4j
public class ArticleAction {
	
	@Autowired
	private IMenuService menuService;

    @Autowired
	private IArticleService articleService;

	/**
	 * 主页
	 * @return
	 */
	@RequestMapping("/")
	public ModelAndView index(ArticleModel articleModel) throws Exception{
        log.info("开始进入主页");
        log.info("打印参数:"+ JSONUtils.toJSON(articleModel));
        articleModel.setTimeScope("month");
        articleModel.setPageNum(1);
		return common(articleModel);
	}
	
	/**
     * 显示最新信息
     * @param pageNum
     * @return
     * @throws Exception
     */
    @RequestMapping("new/{timeScope}/{pageNum}")
    public ModelAndView newArticle(@PathVariable(value="pageNum") int pageNum,@PathVariable("timeScope") String timeScope) throws Exception{
        ArticleModel articleModel = new ArticleModel();
        articleModel.setPageNum(pageNum);   //设置当前页
        articleModel.setTimeScope(timeScope);   //设置时间范围

        ModelAndView modelAndView = common(articleModel);
        return modelAndView;
    }

    /**
     * 显示最新信息
     * @param timeScope
     * @return
     * @throws Exception
     */
    @RequestMapping("new/{timeScope}")
    public ModelAndView newArticleV2(@PathVariable("timeScope") String timeScope) throws Exception{
        ArticleModel articleModel = new ArticleModel();

        articleModel.setTimeScope(timeScope);   //设置时间范围
        ModelAndView modelAndView = common(articleModel);
        return modelAndView;
    }

    private ModelAndView common(ArticleModel articleModel) throws Exception {
        log.info("开始进入显示最新信息");
        log.info("打印参数:"+ JSONUtils.toJSON(articleModel));
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        modelAndView.addObject("menuList", menuService.queryMenuList());     //设置菜单
        modelAndView.addObject("articleList", articleService.queryArticleAll(articleModel));    //内容
        modelAndView.addObject("articleModel",articleModel);    //分页信息
        modelAndView.addObject("articleNewList",articleService.queryArticleNew());  //加载最新
        modelAndView.addObject("articleHeatList",articleService.queryArticleHeat());  //加载最热
        return modelAndView;
    }
	
	/**
	 * 热门
	 * @return
	 */
	@RequestMapping("tj")
	public ModelAndView tj()throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("tj");
		modelAndView.addObject("menuList", menuService.queryMenuList());
		return modelAndView;
	}
	
	/**
	 * 随缘
	 * @return
	 */
	@RequestMapping("random")
	public ModelAndView random()throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("random");
		modelAndView.addObject("menuList", menuService.queryMenuList());
		return modelAndView;
	}
	
	/**
	 * 加油赚
	 * @return
	 */
	@RequestMapping("category/books")
	public ModelAndView books()throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("category/books");
		modelAndView.addObject("menuList", menuService.queryMenuList());
		return modelAndView;
	}
	
	/**
	 * 排行榜
	 * @return
	 */
	@RequestMapping("users")
	public ModelAndView users()throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("users");
		modelAndView.addObject("menuList", menuService.queryMenuList());
		return modelAndView;
	}
	
	/**
	 * 查文章内容
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("article/{articleId}")
	public ModelAndView article(@PathVariable("articleId") Integer articleId) throws Exception{
		ArticleModel articleModel = new ArticleModel();
		articleModel.setArticleId(articleId);

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("article/articleDetail");
		modelAndView.addObject("article", articleService.queryArticleById(articleModel));
		modelAndView.addObject("menuList", menuService.queryMenuList());
        modelAndView.addObject("articleNewList",articleService.queryArticleNew());  //加载最新
        modelAndView.addObject("articleHeatList",articleService.queryArticleHeat());  //加载最热
		return modelAndView;
	}
}
